package com.yun.algorithm.sort;

import java.util.Arrays;

public class CountingSort {
    
    public static void sort(int[] a){
        int max=a[0];
        for (int v : a) {
            if (v > max) {
                max = v;
            }
        }
        int[] count=new int[max+1];
        for (int v : a) {
            count[v]++;
        }
        int k=0;
        for (int i = 0; i < count.length; i++) {
            while (count[i]>0){
                a[k++]=i;
                count[i]--;
            }
        }
    }

    public static void main(String[] args) {
        int[] a = {5,1,1,3,0};
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
